package ch.bfh.abcmmh.view.detailview.minipanels;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JLabel;
import javax.swing.JPanel;

import ch.bfh.abcmmh.view.detailview.streets.StreetPanel;

public class SpeedLimitPanelMouseListener extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	int radius = 50;
	int speedlimit;
	int xOffset;
	JLabel label;
	bigCircle bigC;
	miniCircle miniC;
	StreetPanel streetPanel;

	public SpeedLimitPanelMouseListener(int speedLimit, StreetPanel streetPanel) {
		this.streetPanel = streetPanel;
		this.speedlimit = speedLimit;
		bigC = new bigCircle();
		miniC = new miniCircle();
		this.label = new JLabel(Integer.toString(speedLimit));
		label.setOpaque(false);
		label.setLayout(null);
		label.setBackground(Color.black);
		Dimension size = new Dimension(50, 50);
		setPreferredSize(size);
		setMinimumSize(size);
		setMaximumSize(size);
		setSize(size);
		setLayout(null);
		this.addMouseListener(new MouseEventsListener());
		this.setOpaque(false);
		miniC.add(label);
		this.add(bigC);
		bigC.add(miniC);

	}

	class miniCircle extends JPanel {

		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;

		miniCircle() {
			Dimension size = new Dimension(40, 40);
			setPreferredSize(size);
			setMinimumSize(size);
			setMaximumSize(size);
			setSize(size);
			this.setOpaque(false);
			this.addMouseListener(new MouseEventsListener());

		}

		public void paintComponent(Graphics g) {

			g.setColor(Color.white);
			g.fillRoundRect(0, 0, getWidth() - 1, getHeight() - 1, radius,
					radius);

		}
	}

	class bigCircle extends JPanel {
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;

		public bigCircle() {
			Dimension size = new Dimension(50, 50);
			setPreferredSize(size);
			setMinimumSize(size);
			setMaximumSize(size);
			setSize(size);

			this.setOpaque(false);
			this.addMouseListener(new MouseEventsListener());
		}

		public void paintComponent(Graphics g) {

			g.setColor(Color.red);
			g.fillRoundRect(0, 0, getWidth() - 1, getHeight() - 1, radius,
					radius);

		}
	}

	class MouseEventsListener extends MouseAdapter {

		@Override
		public void mouseReleased(MouseEvent e) {
			int offset = streetPanel.getStreetController().getCurrentPos();
			streetPanel.addSpeedLimit(speedlimit, e.getX() + offset);
			streetPanel.getLogicController().getStreetGenerator()
					.setSpeedLimit(e.getX() + offset, speedlimit);
			streetPanel.getGUIController().getMainFrame().getFullviewPanel()
					.updateAllStreetSequences();

		}

	}

}
